/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package com.hnisi
{
	
	import com.hnisi.skins.LinkAdornerSkin;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.FlexGlobals;

	/*******************************************
	 **** @author huangjixin,@date 2012-7-10, @time 上午10:08:45 **
	 **** 连线边饰器  **
	 *******************************************/
	public class LinkAdorner extends Adorner
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _pressHandle:LinkHandle;
		
		private var _startPoint:Point = new Point();
		
		private var _fallbackStartPoint:Point;
		private var _fallbackEndPoint:Point;
		
		private var _mouseOver:Boolean;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		[SkinPart]
		public var startLinkHandle:LinkHandle;
		[SkinPart]
		public var endLinkHandle:LinkHandle;
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public function LinkAdorner(adornedObject:Node)
		{
			setStyle("skinClass", LinkAdornerSkin);
			
			super(adornedObject);
			adornedObject.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
			adornedObject.addEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		protected function onRollOut(event:MouseEvent):void
		{
			_mouseOver = false;
		}
		
		protected function onRollOver(event:MouseEvent):void
		{
			_mouseOver = true;
		}
		
		protected function onHandleMouseDown(event:MouseEvent):void
		{
			handlePressed(event.currentTarget as DisplayObject, event);
		}
		
		protected function onHandleMouseMove(event:MouseEvent):void
		{			
			if(_pressHandle == startLinkHandle){
				Link(adornedObject).fallbackStartPoint.x = Link(adornedObject).mouseX;
				Link(adornedObject).fallbackStartPoint.y = Link(adornedObject).mouseY;
				startLinkHandle.x = Link(adornedObject).fallbackStartPoint.x - startLinkHandle.width; 
				startLinkHandle.y = Link(adornedObject).fallbackStartPoint.y - startLinkHandle.height/2; 
				Link(adornedObject).invalidateDisplayList();
			}
		}
		
		protected function onHandlerMouseUp(event:Event):void
		{
			_pressHandle = null;
			FlexGlobals.topLevelApplication.parent.removeEventListener(MouseEvent.MOUSE_MOVE,onHandleMouseMove);
			FlexGlobals.topLevelApplication.parent.removeEventListener(MouseEvent.MOUSE_UP,onHandlerMouseUp);
		}
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		override protected function partRemoved(partName:String, instance:Object):void{
			super.partRemoved(partName,instance);
			
			if(instance == startLinkHandle){
				startLinkHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == endLinkHandle){
				endLinkHandle.removeEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			
			if(instance == startLinkHandle){
				startLinkHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
			if(instance == endLinkHandle){
				endLinkHandle.addEventListener(MouseEvent.MOUSE_DOWN,onHandleMouseDown);
			}
		}
		
		override public function play():void
		{
			/*this.width = this.adornedObject.width;
			this.height = this.adornedObject.height;*/
			var point:Point = new Point();
			point = this.adornedObject.localToGlobal(point);
			point = FlexGlobals.topLevelApplication.globalToLocal(point);
			if(!FlexGlobals.topLevelApplication.contains(this)){
				FlexGlobals.topLevelApplication.addElement(this);
			}
			
			this.x = point.x;
			this.y = point.y;
			
			AdonerPlayer.playIng = true;
		}//play结束
		
		override protected function handlePressed(handle:DisplayObject, event:MouseEvent):void{
			_pressHandle = handle as LinkHandle;
			
			this._startPoint.x = mouseX;
			this._startPoint.y = mouseY;
			this._startPoint  = this.localToGlobal(_startPoint);
			
			_fallbackStartPoint = Link(adornedObject).fallbackStartPoint;
			_fallbackEndPoint = Link(adornedObject).fallbackEndPoint;
			
			FlexGlobals.topLevelApplication.parent.addEventListener(MouseEvent.MOUSE_MOVE,onHandleMouseMove);
			FlexGlobals.topLevelApplication.parent.addEventListener(MouseEvent.MOUSE_UP,onHandlerMouseUp);
		}
		override public function isMouseNear(tolerance:Number):Boolean{
			if(this.adornedObject.mouseX > -tolerance && this.adornedObject.mouseX < this.adornedObject.width + tolerance &&
				this.adornedObject.mouseY > -tolerance && this.adornedObject.mouseY < this.adornedObject.height + tolerance){
				return true;
			}
			return _mouseOver;
		}
	}//类结束
}//包结束